DELIMITER $$

DROP TRIGGER `${updateTableName!}_insertBefore`$$

CREATE
TRIGGER `${updateTableName!}_insertBefore` BEFORE INSERT ON `${updateTableName!}`
FOR EACH ROW BEGIN
<#list newMapList as newMap>
IF(new.${newMap.updateForeignKeyName!} IS NOT NULL) THEN
SET new.${newMap.updateFieldName!} = IFNULL((SELECT c.${newMap.dataFieldName!} FROM ${newMap.dataTableName!} c WHERE c.${newMap.dataPrimaryKeyName!} = new.${newMap.updateForeignKeyName!}), new.${newMap.updateForeignKeyName!});
<#if ((newMap.updateFieldName1!) != '')>
SET new.${newMap.updateFieldName1!} = IFNULL((SELECT c.${newMap.dataFieldName1!} FROM ${newMap.dataTableName!} c WHERE c.${newMap.dataPrimaryKeyName!} = new.${newMap.updateForeignKeyName1!}), new.${newMap.updateForeignKeyName1!});
</#if>
<#if ((newMap.updateFieldName2!) != '')>
SET new.${newMap.updateFieldName2!} = IFNULL((SELECT c.${newMap.dataFieldName2!} FROM ${newMap.dataTableName!} c WHERE c.${newMap.dataPrimaryKeyName!} = new.${newMap.updateForeignKeyName2!}), new.${newMap.updateForeignKeyName2!});
</#if>
<#if ((newMap.updateFieldName3!) != '')>
SET new.${newMap.updateFieldName3!} = IFNULL((SELECT c.${newMap.dataFieldName3!} FROM ${newMap.dataTableName!} c WHERE c.${newMap.dataPrimaryKeyName!} = new.${newMap.updateForeignKeyName3!}), new.${newMap.updateForeignKeyName3!});
</#if>
<#if ((newMap.updateFieldName4!) != '')>
SET new.${newMap.updateFieldName4!} = IFNULL((SELECT c.${newMap.dataFieldName4!} FROM ${newMap.dataTableName!} c WHERE c.${newMap.dataPrimaryKeyName!} = new.${newMap.updateForeignKeyName4!}), new.${newMap.updateForeignKeyName4!});
</#if>
END IF;
</#list>
END;
$$

DELIMITER ;

